package com.yuncheng.config;

import com.baomidou.mybatisplus.annotation.FieldStrategy;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * ProtectedMybatisConfig
 *
 * @date 2023/5/7
 */
@Component
public class ProtectedMybatisConfig implements InitializingBean {

    @Autowired
    private MybatisPlusProperties mybatisPlusProperties;

    private void checkMybatisUpdateStrategy() {
        FieldStrategy fieldStrategy = mybatisPlusProperties.getGlobalConfig().getDbConfig().getUpdateStrategy();
        if (FieldStrategy.NOT_NULL.equals(fieldStrategy) || FieldStrategy.NOT_EMPTY.equals(fieldStrategy)) {
            // 基础功能代码只支持这两个设置
        } else {
            // 其他设置会引起基础功能代码update数据库记录异常，直接终止应用启动
            throw new RuntimeException("基础功能代码只支持mybatis-plus.global-config.db-config.update-strategy=NOT_NULL或者NOT_EMPTY");
        }
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        checkMybatisUpdateStrategy();
    }
}
